<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="600px" :title="`${drawerProps.title}用户`" @open="drawerOpen">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户头像" prop="avatar">
        <UploadImg
          :only-btn="false"
          v-model:image-url="drawerProps.row!.avatar"
          width="135px"
          height="135px"
          :file-size="3"
          border-radius="50%"
          :show-success-msg="true"
        >
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="drawerProps.row!.userName" placeholder="请填写用户姓名" clearable></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item prop="password">
            <template #label>
              <span style="display: flex; align-items: center">
                <el-tooltip content="密码为空时，默认密码为Aa000000" placement="top">
                  <el-icon :size="22"><QuestionFilled /></el-icon>
                </el-tooltip>
                用户密码
              </span>
            </template>

            <el-input v-model="drawerProps.row!.password" placeholder="请填写用户密码" clearable></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="用户昵称" prop="nickName">
            <el-input v-model="drawerProps.row!.nickName" placeholder="请填写用户姓名" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="所属部门" prop="deptId">
            <el-tree-select node-key="id" v-model="drawerProps.row!.deptId" :data="deptTree" placeholder="请选择归属部门" />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="用户角色" prop="roleIds">
            <el-select v-model="drawerProps.row!.roleIds" multiple placeholder="请选择角色">
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select v-model="drawerProps.row!.sex" placeholder="请选择性别" clearable>
              <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="drawerProps.row!.status">
              <el-radio v-for="item in userStatus" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="drawerProps.row!.phonenumber" placeholder="请填写手机号" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="drawerProps.row!.email" placeholder="请填写邮箱" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";

import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { genderType, userStatus } from "@/utils/serviceDict";
import { SystemApi, UserApi } from "@/api/modules";
import { IDept } from "../../dept/components/dept.interface";

const rules = reactive({
  // avatar: [{ required: true, message: "请上传用户头像" }],
  userName: [{ required: true, message: "请填写用户姓名" }],
  // sex: [{ required: true, message: "请选择性别" }],
  // idCard: [{ required: true, message: "请填写身份证号" }],
  // email: [{ required: true, message: "请填写邮箱" }],
  // address: [{ required: true, message: "请填写居住地址" }],
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {},
});
const deptTree = ref([]);
const roleList = ref([]);

const getFormTreeData = async () => {
  const deptRes: any = await UserApi.getUserDeptTree();
  deptTree.value = deptRes.data;
  const roleRes: any = await SystemApi.getRoleList({ status: 0 });
  roleList.value = roleRes.data.list;
  // deptTree.value = deptRes;
};
getFormTreeData();

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  if (params.title == "新增") {
    drawerProps.value.row = {
      userName: "",
      sex: undefined,
      idCard: "",
      email: "",
      address: "",
      status: 0,
      avatar: "",
      password: "Aa000000",
    };
  }

  drawerVisible.value = true;
};
const drawerOpen = () => {
  UserApi.getUserPostAndRole();
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams,
});
</script>
